import tkinter as tk
import random

# 游戏窗口大小
WIDTH = 600
HEIGHT = 400
BALL_RADIUS = 10
PADDLE_WIDTH = 100
PADDLE_HEIGHT = 10
PADDLE_SPEED = 10
OBSTACLE_WIDTH = 30
OBSTACLE_HEIGHT = 15
OBSTACLE_ROWS = 3
OBSTACLE_COLS = 6
OBSTACLE_SPACING = 50

class BallGame:
    def __init__(self, master):
        self.master = master
        self.canvas = tk.Canvas(master, width=WIDTH, height=HEIGHT, bg="black")
        self.canvas.pack()
        
        self.ball = self.create_ball()
        self.paddle = self.create_paddle()
        self.obstacles = self.create_obstacles()
        
        self.ball_dx = 5
        self.ball_dy = -5
        self.game_over = False
        
        self.master.bind("<Left>", self.move_paddle_left)
        self.master.bind("<Right>", self.move_paddle_right)
        
        self.update_game()

    def create_ball(self):
        x = WIDTH // 2
        y = HEIGHT // 2
        return self.canvas.create_oval(x - BALL_RADIUS, y - BALL_RADIUS, x + BALL_RADIUS, y + BALL_RADIUS, fill="white")

    def create_paddle(self):
        x = (WIDTH - PADDLE_WIDTH) // 2
        y = HEIGHT - PADDLE_HEIGHT - 10
        return self.canvas.create_rectangle(x, y, x + PADDLE_WIDTH, y + PADDLE_HEIGHT, fill="blue")

    def create_obstacles(self):
        obstacles = []
        for row in range(OBSTACLE_ROWS):
            for col in range(OBSTACLE_COLS):
                x1 = col * (OBSTACLE_WIDTH + OBSTACLE_SPACING)
                y1 = row * (OBSTACLE_HEIGHT + OBSTACLE_SPACING) + 50
                x2 = x1 + OBSTACLE_WIDTH
                y2 = y1 + OBSTACLE_HEIGHT
                obstacle = self.canvas.create_rectangle(x1, y1, x2, y2, fill="red")
                obstacles.append(obstacle)
        return obstacles

    def move_paddle_left(self, event):
        if not self.game_over:
            self.canvas.move(self.paddle, -PADDLE_SPEED, 0)
            self.canvas.update()

    def move_paddle_right(self, event):
        if not self.game_over:
            self.canvas.move(self.paddle, PADDLE_SPEED, 0)
            self.canvas.update()

    def update_game(self):
        if self.game_over:
            return

        self.canvas.move(self.ball, self.ball_dx, self.ball_dy)
        ball_coords = self.canvas.coords(self.ball)
        paddle_coords = self.canvas.coords(self.paddle)

        # 检查小球是否碰到边界
        if ball_coords[0] <= 0 or ball_coords[2] >= WIDTH:
            self.ball_dx = -self.ball_dx
        if ball_coords[1] <= 0:
            self.ball_dy = -self.ball_dy
        if ball_coords[3] >= HEIGHT:
            self.game_over = True
            messagebox.showinfo("游戏结束", "游戏结束！")
            self.master.quit()
            return

        # 检查小球是否碰到挡板
        if (paddle_coords[0] < ball_coords[2] and paddle_coords[2] > ball_coords[0] and
            paddle_coords[1] < ball_coords[3] < paddle_coords[3]):
            self.ball_dy = -self.ball_dy

        # 检查小球是否碰到障碍物
        for obstacle in self.obstacles:
            obstacle_coords = self.canvas.coords(obstacle)
            if (obstacle_coords[0] < ball_coords[2] and obstacle_coords[2] > ball_coords[0] and
                obstacle_coords[1] < ball_coords[3] < obstacle_coords[3]):
                self.ball_dy = -self.ball_dy
                self.canvas.delete(obstacle)
                self.obstacles.remove(obstacle)
                break

        self.master.after(30, self.update_game)

if __name__ == "__main__":
    root = tk.Tk()
    game = BallGame(root)
    root.mainloop()